package practice;

import java.util.Scanner;

public class practice02 {
    /**
     * 数组中两个字符串的最小距离
     * @param args
     */
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        in.nextLine();
        String[] str = in.nextLine().split(" ");
        String s1 = str[0], s2 = str[1];
        int prev1 = -1, prev2 = -1, ret = Integer.MAX_VALUE;
        for (int i = 0; i < n; i++) {
            String s = in.nextLine();
            if (s.equals(s1)) {
                if (prev2 != -1) {
                    ret = Math.min(ret, i - prev2);
                }
                prev1 = i;
            } else if (s.equals(s2)) {
                if (prev1 != -1) {
                    ret = Math.min(ret, i - prev1);
                }
                prev2 = i;
            }
        }
        System.out.println(ret == Integer.MAX_VALUE ? -1 : ret);
    }

    /**
     * 最小花费爬楼梯
     * @param args
     */
    public static void main2(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] cost = new int[n];
        for (int i = 0; i < n; i++) {
            cost[i] = in.nextInt();
        }
        // 1. 创建 dp 表
        // 2. 初始化
        // 3. 填表
        // 4. 返回值
        int[] dp = new int[n + 1];
        if (n == 1) {
            System.out.println(cost[0]);  // 必须支付cost[0]到达顶部
            return;
        }
        for (int i = 2; i <= n; i++) {
            dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
        }
        System.out.println(dp[n]);
    }

    /**
     * 牛牛的快递
     */
    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        double a = in.nextDouble();
        char ch = in.next().charAt(0);
        int ret = 0;
        if (a < 1) {
            ret = 20;
        } else {
            ret = 20 + (int)Math.ceil((a - 1));
        }
        if (ch == 'y') ret += 5;
        System.out.println(ret);
    }
}
